var express = require('express');
var router = express.Router();
var { essayModel,essayTypeModel, adoptModel, varietyTypeModel, collarModel} = require('../../models/db')
/* GET home page. */
router.get('/essay', async (req, res, next)=>{
  const {val} = req.query
  const typeData = await essayTypeModel.find()
  let data = []
  const name = new RegExp(val)
  if(val){
    data = await essayModel.find({
      name : name
    }).populate('type')
  }else{
    data = await essayModel.find().populate('type')
  }
  res.send({
    code : 200,
    data,
    typeData
  })
});

router.get('/adopt', async (req, res, next)=>{
  const data = await adoptModel.find().populate('variety')
  res.send({
    code : 200,
    data
  })
});

router.get('/collar', async (req, res, next)=>{
  const {val} = req.query
  let data = []
  const name = new RegExp(val)
  if(val){
    data = await collarModel.find({
      name : name
    })
  }else{
    data = await collarModel.find()
  }
  res.send({
    code : 200,
    data
  })
})

module.exports = router;
